package com.project.collectAPI.entity.multiUpdateProduct;


import com.alibaba.fastjson.JSONObject;
import com.project.collectAPI.comm.SpiderTools;
import com.project.collectAPI.entity.MeiTuan.SystemParam;
import lombok.Data;
import org.apache.http.client.config.RequestConfig;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

//批量创建/更新商品
@Data
public class MultiUpdateProduct {


    private String reqAPI="https://waimaiopen.meituan.com/api/v1/retail/batchinitdata";


    //APP方门店id，即商家中台系统里门店的编码。如商家在操作绑定门店至开放平台应用中时，未绑定三方门店id信息，则默认APP方门店id与美团门店id相同。
    private String app_poi_code;

    //商品图片json字符串集合
    private String food_data;

    //统一的时间戳
    private String app_id;

    //统一的时间戳
    private Long timestamp;

    //签名
    private String signValue;


    /*批量创建接口签名*/
    public String getSign() throws Exception {
        SystemParam systemParam=new SystemParam();
        Map<String,Object> map=new HashMap<>();
        //System.out.println("getSign() SystemParam.timestamp===>"+timestamp);
        map.put("timestamp",timestamp);  //timestamp尽量统一用一次
        map.put("app_id",systemParam.getAppId());
        map.put("app_poi_code",systemParam.getApp_poi_code());
        map.put("food_data",food_data);
        return systemParam.getSign(map,reqAPI); //获取签名
    }


    /*请求*/
    public String startMulitCreate() throws Exception {

        SystemParam systemParam=new SystemParam();
        //Post请求2
        Map<String, String> applicationParamsMap=new HashMap<>();
        applicationParamsMap.put("app_id",systemParam.getAppId());
        applicationParamsMap.put("timestamp",String.valueOf(timestamp));
        applicationParamsMap.put("sig",signValue);
        applicationParamsMap.put("app_poi_code",systemParam.getApp_poi_code());
        //applicationParamsMap.put("food_data", URLEncoder.encode(food_data,"UTF-8"));
        applicationParamsMap.put("food_data", food_data);
        RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
        // ConnectTimeout:连接超时.连接建立时间，三次握手完成时间.
        requestConfigBuilder.setConnectTimeout( 30000* 1000);
        String res= SpiderTools.requestOfPost(reqAPI, applicationParamsMap,requestConfigBuilder);
        System.out.println("请求参数:");
        System.out.println(applicationParamsMap);
        System.out.println("mulitUploadProduct批量创建/更新接口结果================>");
        System.out.println(res);
        JSONObject rct=JSONObject.parseObject(res);
        String data= (!Objects.isNull(rct.get("data")))?rct.getString("data"):null;

        return data;
    }


}
